package DanLiMoShi;


/**
 * 使用同步机制将单例模式中的懒汉式改写为线程安全的
 */
//public class Bank {
//    private Bank(){
//
//    }
//    private static Bank instance = null;
//    //public static synchronized Bank getInstance(){或
//    public static  Bank getInstance(){
//        //方式一：效率不高
//        //一个线程执行，其余的都干等
//        synchronized (Bank.class) {
//            if(instance == null){
//                instance = new Bank();
//            }
//            return instance;
//        }
//    }
//}
public class Bank {
    private Bank() {

    }

    private static Bank instance = null;

    public static Bank getInstance() {
//方式二：
        if (instance == null) {
            synchronized (Bank.class) {
                if (instance == null) {
                    instance = new Bank();
                }
            }
        }
        return instance;
    }
}